/*
 * IkaString.cpp
 */

#include "IkaString.h"

namespace atomic {

//*********************************************************************
IkaString::IkaString() {
	 mySize = 0;
	 myText = NULL;
}

//*********************************************************************
IkaString::IkaString(const char *text) {
	mySize = Size(text);
	myText = Copy(text);
}

//*********************************************************************
IkaString::IkaString(const atomic::IkaString & text){
	 mySize = text.mySize;
	 myText = text.myText;
}

//*********************************************************************
IkaString::~IkaString(){
	delete[] myText;
}

//*********************************************************************
int IkaString::Size() {
	return mySize;
}

//*********************************************************************
int IkaString::Size(const char* text){
	int size=0;
	while(text[size] != '\0'){
		size++;
	}
	return size;
}

//*********************************************************************
char* IkaString::Copy(const char *text) {
	int size = Size(text);
	char *copy = new char[size+1];

	for(int i=0; i<size; i++){
		copy[i]=text[i];
	}
	copy[size] = '\0';
	return copy;
}

//*********************************************************************
void IkaString::Print() {
	printf("%s \n", myText);
}

//*********************************************************************
char* IkaString::Text(){
	return myText;
}

//*********************************************************************
void IkaString::Clear(){
	myText = NULL;
	mySize = 0;
}


//*********************************************************************
atomic::IkaString IkaString::operator=(const char *text){
	delete[] myText;
	mySize = Size(text);
	myText = Copy(text);
	return *this;
}

//*********************************************************************
atomic::IkaString IkaString::operator=(const atomic::IkaString &text){
	delete[] myText;
	mySize = text.mySize;
	myText = text.myText;
	return *this;
}

//*********************************************************************
atomic::IkaString IkaString::operator+(const char *text){
	int size1 = mySize;
	int size2 = Size(text);
	int fullSize = size1+size2;
	char *fullText = new char[fullSize+1];

	for(int i=0; i<size1; i++){
		fullText[i] = myText[i];
	}
	for(int i=size1; i<fullSize; i++){
		fullText[i] = text[i-size1];
	}
	fullText[fullSize] = '\0';

	//Res
	atomic::IkaString res(fullText);
	delete[] fullText;

	return res;

}

//*********************************************************************
atomic::IkaString IkaString::operator+(const atomic::IkaString &text){
	int size1 = mySize;
	int size2 = text.mySize;
	int fullSize = size1+size2;
	char *fullText = new char[fullSize+1];

	for(int i=0; i<size1; i++){
		fullText[i] = myText[i];
	}
	for(int i=size1; i<fullSize; i++){
		fullText[i] = text.myText[i-size1];
	}
	fullText[fullSize] = '\0';

	//Res
	atomic::IkaString res(fullText);
	delete[] fullText;

	return res;

}

//*********************************************************************
template<class T>
void IkaString::Add(const T& text){
	//TODO avec cast pour string, int, float, et IkaText
}

//*********************************************************************

}
